/* 公共request 方法 */
const config = require('@/utils/API.js')
const ajax = ({
	url,
	params,
	success,
	method = "post",
	isOP = 0
}) => {
	// 请求头存储商户id
	let header = {
		'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
		'storeid': 6
	}

	let merchantId = uni.getStorageSync('merchantId') || null
	if (merchantId) {
		params['merchantId'] = merchantId
	}

	// 判断是否有网络
	let isNetWork = true
	uni.getNetworkType({
		success: function(res) {
			if (res.networkType == 'none') {
				isNetWork = false
			}
		}
	})

	if (isNetWork) {
		return new Promise(function(resolve, reject) {
				uni.request({
					url: config.apiUrl + url,
					method: method,
					data: params,
					header: header,
					sslVerify: false,
					success: (res) => {
						if (res.data.result === 'error' || res['statusCode'] !== 200) {
							if (res['statusCode'] === 401) {
								uni.showToast({
									title: '信息失效，请重新登录',
									icon: 'none',
									duration: 2000,
									mask: true
								})
								// setTimeout(() => {
								// 	uni.reLaunch({
								// 		url: '/pagesIndex/login/login'
								// 	})
								// }, 500)
							} else {
								uni.showToast({
									title: res.data.msg || res['statusCode'],
									icon: 'none',
									duration: 2000,
									mask: true
								})
							}
						}
						resolve(res.data)
					},
					fail: function(res) {
						uni.showToast({
							title: '网络延迟，请稍后重试',
							icon: 'none',
							duration: 2000,
							mask: true
						})
					}
				})
			})
			.catch((res) => {})
	} else {
		uni.showToast({
			title: '请开启网络',
			icon: 'none',
			duration: 2000,
			mask: true
		})
	}
}
/* 公共showTotast  loading 方法 */
module.exports = {
	// formatTime: formatTime,
	ajax: ajax
}
